<h1>Store Visit - Order #<?php echo $order->id?> Details
</h1>
<p>&nbsp;</p>
<form class="form" action="/orders/success/<?php echo $order->id ?>" role="form" method="POST">
	<div id="step1">
		<div class="col-lg-12">
			<div class="row">
				<div class="col-lg-5 col-sm-12">
					<div class="form-group">
						<label class="control-label" for="inputName">Store Name</label>
						<div class="controls">
							<input class="form-control" name="data[store_title]" type="text" id="inputName" value="<?php if($distributor != null) echo $distributor->title;?>" disabled="disabled">
						</div>
					</div>
					<div class="form-group">
						<label class="control-label" for="inputTemplate">Order Form Used</label>
						<div class="controls">
							<?php $template = Templates::model()->findByPk($order->template_id)?>
							<input class="form-control" name="data[template_title]" type="text" id="inputTemplate" value="<?php if($template != null) echo $template->title;?>" disabled="disabled">
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="col-lg-12">
			<div id="products">
				<?php 
				$settings = CJSON::decode($template->settings); 
                $minqties = CJSON::decode($template->min_quantity);
                ?>
                    <?php 
                        if(isset($isset_category)){
							//var_dump($isset_category);
                            $total = 0;
                            foreach($isset_category as $key){ 
								if(!empty($settings)){
									$current_discount=array();
									foreach($settings as $setting => $value_setting){
										if($setting == $key)
											$current_discount =$value_setting;
									}
								}
								//var_dump($current_discount);
								if(!empty($minqties)){
									$current_minqty=array();
									foreach($minqties as $minqty => $value_minqty){
										if($minqty == $key)
											$current_minqty =$value_minqty;
									}
								}
								//var_dump($current_minqty);
                                $category = ProductsCategories::model()->findByPk($key);
                            ?>
                            
                                <table class="table table-striped table-hover">
            					    <tr>
            					        <th><div align="center">Photo</div></th>
            					        <th>Title</th>
            					        <th><div align="center">Promo</div></th>
            					        <th><div align="center">Pickups</div></th>
            					        <th><div align="center">Quantity</div></th>
            					        <th><div align="center">Base Price</div></th>
            					        <th><div align="center">Total</div></th>
            					    </tr>
                                    <h3><?php echo $category->title; ?></h3>
                                   <?php 
                                    $sql_tmp = 'SELECT * FROM products WHERE  category_id = '. $category->id.' and id IN ('. implode(", ",$result). ')' ;
                                   
                                    $product_description = Yii::app()->db->createCommand($sql_tmp)->queryAll();  ?>
                                    <?php foreach($product_description as $item){ 
										
                                        $order_quantity = OrdersQuantity::model()->findByAttributes(array('product_id'=>$item['id'],'order_id'=>$order->id)); 
                                        if(isset($order_quantity)){
                                        if(($order_quantity->quantity > 0) || ( $order_quantity->promo != null) || ($order_quantity->pickup !=null)){ ?>
                                         
                                         <tr>
        					                <td><div align="center"><img width="32px" height="32px" src="/public/products/<?php echo $item['photo']?>"/></div></td>
        					                <td><?php echo $item['title']?></td>
        					                <td><div align="center"><?php if($order_quantity->promo != 0){ echo $order_quantity->promo;  }else{ echo ""; }?></div></td>
        					                <td><div align="center"><?php if($order_quantity->pickup != 0){ echo $order_quantity->pickup;  }else{ echo ""; }?></div></td>
        					                <td><div align="center"><?php if($order_quantity->quantity != 0){ echo $order_quantity->quantity;  }else{ echo ""; } ?></div></td>
        					                <td><div align="center">$<?php echo $item['price']?></div></td>
        					                <td><div align="center">$
        					                	<?php
													if(isset($current_minqty[0])){
														
														if($order_quantity->quantity < $current_minqty[0]){
															$price = $order_quantity->quantity * $item['price'];
															//var_dump($price);
														}else {
															if(isset($current_minqty[1])){
																//var_dump($current_minqty[1]);
																if($current_minqty[0] <= $order_quantity->quantity && $order_quantity->quantity < $current_minqty[1]){
																	$price = round($order_quantity->quantity * ($item['price'] - ($current_discount[0]/1000)*$item['price']),2);
																	//var_dump($price);
																}else if($current_minqty[1] <= $order_quantity->quantity){
																	$price = round($order_quantity->quantity * ($item['price'] - ($current_discount[1]/1000)*$item['price']),2);
																	//var_dump($price);
																}
															}else if ( $current_minqty[0]<= $order_quantity->quantity){
																$price = round($order_quantity->quantity * ($item['price'] - ($current_discount[0]/1000)*$item['price']),2);
																//var_dump($price);
															}
														}
													}
													
													$total+= $price;
													echo $price; 
        					                	?>
        					                </div></td>
        					            </tr>
                                    <?php  
										}
									}
                                    }
							}
									?>
									</table>
                   <?php
					} 
				   ?>
                   <table class="table table-striped table-hover">
                     <tr>
                     <td style="width: 80%;"><strong></strong></td>
                        <td class="text-right" style="width: 20%;">
                            <strong>Total: $<span id="final"><?php echo $total; ?></span></strong>
                            <input type="hidden" name="data[final]" id="final-input"/>
                        </td>
                    </tr>
                </table> 
					<?php
						$checked = '';
						$checked2 = '';
						if($order->delivered==1)
							$checked = 'checked';
						else
							$checked2 = 'checked';
					?>
					<h3><strong>Important Question must answer</strong></h3>
					<p><strong>This order will be delivered by me today?  
				    <input type="radio" name="data[delivered]" value="1" <?php echo $checked;?> id="delivered">
				    <label for="delivered">Yes</label>
				    <input type="radio" name="data[delivered]" value="0" <?php echo $checked2;?> id="delivered2">
				    <label for="delivered2">No</label>
					</strong></p>
					<p>&nbsp;</p>
					<p><a href="/orders/edit/<?php echo $order->id?>" class="btn btn-default">Edit Order</a>
					
                <button type="submit" class="btn btn-success">Confirm Order</button>
				 
		  </div>
	  </div>
  </div>
</form>